const CONFIG = require('config.js')
const AUTH = require('utils/auth')
const WXAPI = require('apifm-wxapi')
App({
  globalData: {
    statusBarHeight: 0,
    titleBarHeight: 0,
    loginStatus: false,
    isConnected: true
  },
  onLaunch: function () {
    const that = this;
    // 检测新版本
    const updateManager = wx.getUpdateManager()
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    /**
     * 初次加载判断网络情况
     * 无网络状态下根据实际情况进行调整
     */

    wx.getNetworkType({
      success(res) {
        const networkType = res.networkType
        if (networkType === 'none') {
          that.globalData.isConnected = false
          wx.showToast({
            title: '当前无网络',
            icon: 'loading',
            duration: 2000
          })
        }
      }
    });
    /**
     * 监听网络状态变化
     * 可根据业务需求进行调整
     */
    wx.onNetworkStatusChange(function (res) {
      if (!res.isConnected) {
        that.globalData.isConnected = false
        wx.showToast({
          title: '网络已断开',
          icon: 'loading'
        })
      } else {
        that.globalData.isConnected = true
        // wx.hideToast()
      }
    });
    // 获取手机顶部的高度
    // wx.getSystemInfo({
    //   success: (res) => {
    //     const titleBar = wx.getMenuButtonBoundingClientRect()
    //     const titleBarHeight = (titleBar.top - res.statusBarHeight) * 2 + titleBar.height
    //     this.globalData.statusBarHeight = res.statusBarHeight
    //     this.globalData.titleBarHeight = titleBarHeight
    //     console.log('statusBarHeight:', res.statusBarHeight, 'titleBarHeight:', titleBarHeight, 'titleBar:', titleBar);
    //   },
    // })
    // 获取用户code
    wx.login({
      success: function (res) {
        // console.log("wx.login:", res);
        wx.setStorageSync('code', res.code)
      }
    })
    
  },
  onShow(e) {
  },
  
})